package io.gitee.falllee.dao.config;

import org.springframework.core.NamedThreadLocal;

/**
 * 保存多线程的DAO配置类
 * 
 * @author batty
 * @date 2019/05/16
 */
public final class DaoConfigContextHolder {

    private DaoConfigContextHolder() {
    }

    private static final ThreadLocal<String> HOLDER = new NamedThreadLocal<>("DaoConfig");
    
    private static final ThreadLocal<Boolean> INCLUDE_DISABLE_HOLDER = new NamedThreadLocal<>("DaoIncludeDisable");

    private static final ThreadLocal<Boolean> DISABLE_CACHE_HOLDER = new NamedThreadLocal<>("DaoDisableCache");
    
    private static final ThreadLocal<Boolean> ADD_GLOBAL_FILTER_HOLDER = new NamedThreadLocal<>("DaoAddGlobalFilter");

    public static String getDaoConfig() {
        return HOLDER.get();
    }

    public static void setDaoConfig(String daoConfig) {
        HOLDER.set(daoConfig);
    }

    public static boolean getIncludeDisable() {
        return INCLUDE_DISABLE_HOLDER.get() != null && INCLUDE_DISABLE_HOLDER.get();
    }

    public static void setIncludeDisable(boolean includeDisable) {
        INCLUDE_DISABLE_HOLDER.set(includeDisable);
    }
    
    public static boolean getDisableCache() {
        return DISABLE_CACHE_HOLDER.get() != null && DISABLE_CACHE_HOLDER.get();
    }

    public static void setDisableCache(boolean disableCache) {
        DISABLE_CACHE_HOLDER.set(disableCache);
    }
    
    public static boolean getAddGlobalFilter() {
        return ADD_GLOBAL_FILTER_HOLDER.get() == null || ADD_GLOBAL_FILTER_HOLDER.get();
    }

    public static void setAddGlobalFilter(boolean addGlobalFilter) {
        ADD_GLOBAL_FILTER_HOLDER.set(addGlobalFilter);
    }

    public static void clearDaoConfig() {
        HOLDER.remove();
        clearIncludeDisable();
        clearDisableCache();
        clearAddGlobalFilter();
    }
    
    public static void clearIncludeDisable() {
        INCLUDE_DISABLE_HOLDER.remove();
    }
    
    public static void clearDisableCache() {
        DISABLE_CACHE_HOLDER.remove();
    }
    
    public static void clearAddGlobalFilter() {
        ADD_GLOBAL_FILTER_HOLDER.remove();
    }
}
